﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using Persistence.Dao;
using Domain;
using Persistence;
namespace web.manage
{
    public partial class Product_Product_Operate : adminBase
    {
        int pid = DNTRequest.GetQueryInt("id", 0);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindDdl();
                if (pid > 0)
                {
                    Product p = Singleton<ProductDao>.Instance.GetModel(pid);
                    txtMarketPrice.Value = p.markedPrice.ToString();
                    txtMemberPrice.Value = p.memberPrice.ToString();
                    txtMinOrder.Value = p.minOrderNumber.ToString();
                    txtIntro.Value = p.productIntro;
                    txtNo.Value = p.productModelNO;
                    txtName.Value = p.productName;
                    txtShowOrder.Value = p.productShowOrder.ToString();
                    txtIndexOrder.Value = p.productIndexOrder.ToString();
                    txtListOrder.Value = p.productListOrder.ToString();
                    txtOther.Value = p.productOtherInfo;
                    txtStoreNo.Value = p.productStockNumber.ToString();
                    ddlType.SelectedValue = p.productType.ToString();
                    if (string.IsNullOrEmpty(p.productImages))
                    {
                        hlImage.Visible = false;
                    }
                    else
                    {
                        hlImage.NavigateUrl = p.productImages;
                    }
                }
            }
        }

        private void bindDdl()
        {
            ddlType.DataSource = Singleton<ProductTypeDao>.Instance.GetModels("*", "productTypeLevel=0");
            ddlType.DataTextField = "productTypeName";
            ddlType.DataValueField = "productTypeID";
            ddlType.DataBind();
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Product p;
            if (pid > 0)
            {
                p = Singleton<ProductDao>.Instance.GetModel(pid);
            }
            else
            {
                p = new Product();
            }
            p.addTime = DateTime.Now;
            p.markedPrice = Utils.StrToDecimal(txtMarketPrice.Value, 0);
            p.memberPrice = Utils.StrToDecimal(txtMemberPrice.Value, 0);
            p.minOrderNumber = Utils.StrToInt(txtMinOrder.Value, 0);
            p.productIntro = txtIntro.Value.Trim();
            p.productModelNO = txtNo.Value;
            p.productName = txtName.Value.Trim();
            p.productShowOrder = Utils.StrToInt(txtShowOrder.Value, 0);
            p.productIndexOrder = Utils.StrToInt(txtIndexOrder.Value, 0);
            p.productListOrder = Utils.StrToInt(txtListOrder.Value, 0);
            p.productOtherInfo = txtOther.Value.Trim();
            p.productStockNumber = Utils.StrToInt(txtStoreNo.Value, 0);
            p.productType = Utils.StrToInt(ddlType.SelectedValue, 0);
            if (fileImage.Value != "")
            {
                UpLoad u = new UpLoad();
                u.Path = "uploadfile/picture/big";
                u.ModifyFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond;
                p.productImages = u.SaveOnlyFileName(fileImage);
                Thumbnail.MakeThumbnailImage(Server.MapPath("/uploadfile/picture/big/" + p.productImages),
                    Server.MapPath("/uploadfile/picture/small/" + p.productImages),
                    140, 183);
            }
            if (pid > 0)
            {
                Singleton<ProductDao>.Instance.Update(p);
            }
            else
            {
                Singleton<ProductDao>.Instance.Insert(p);
            }
            Message.alert("操作成功");



        }
    }
}
